///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_ZSEQUENCEOP_H_
#define _BUOLA_ZZ_ZSEQUENCEOP_H_

#include <buola/zz/zbase.h>
#include <buola/zz/detail/sequence.h>
#include <buola/zz/detail/enablers.h>

namespace buola { namespace zz {

template<typename... tElems>
class ZSequenceOp : public ZBase<ZSequenceOp<tElems...>>
{
public:
    typedef std::tuple<tElems...> tElemTuple;

    template<typename tIt>
    struct tAttribute
    {
        typedef typename detail::MSequenceAttr<tIt,tElems...> tSequenceAttr;
        typedef typename tSequenceAttr::tAttribute tType;
        typedef typename tSequenceAttr::tCollapseType tCollapseType;
    };
    
    ZSequenceOp(const ZSequenceOp &pRH)
        :   mElems(pRH.mElems)
    {}

    ZSequenceOp(ZSequenceOp &pRH)
        :   mElems(pRH.mElems)
    {}

    ZSequenceOp(ZSequenceOp &&pRH)
        :   mElems(std::move(pRH.mElems))
    {}

    template<typename... tArgs>
    ZSequenceOp(detail::NNaryExplicit,tArgs&&... pArgs)
        :   mElems(std::forward<tArgs>(pArgs)...)
    {}
    
    template<typename tLH,typename tRH>
    ZSequenceOp(detail::NNaryCombineLeft,tLH &&pLH,tRH &&pRH)
        :   mElems(std::tuple_cat(detail::nary_extract_tuple(std::forward<tLH>(pLH)),
                                  std::make_tuple(std::forward<tRH>(pRH))))
    {}

    template<typename tLH,typename tRH>
    ZSequenceOp(detail::NNaryCombineRight,tLH &&pLH,tRH &&pRH)
        :   mElems(std::tuple_cat(std::make_tuple(std::forward<tLH>(pLH)),
                                  detail::nary_extract_tuple(std::forward<tRH>(pRH))))
    {}

    template<typename tLH,typename tRH>
    ZSequenceOp(detail::NNaryCombineBoth,tLH &&pLH,tRH &&pRH)
        :   mElems(std::tuple_cat(detail::nary_extract_tuple(std::forward<tLH>(pLH)),
                                  detail::nary_extract_tuple(std::forward<tRH>(pRH))))
    {}

    template<int pElem,int pTarget,typename tIt,typename tAttr,typename tCtx,typename tSk>
    typename std::enable_if<pElem==sizeof...(tElems)-1,bool>::type
    ParseImpl(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx &pCtx,const tSk &pSk,UUnused &pDummy) const
    {
        typedef std::is_same<UUnused,typename std::tuple_element<pElem,tElemTuple>::type::template tAttribute<tIt>::tType> tIsUnused;

        return std::get<pElem>(mElems).Parse(pB,pE,detail::MSequenceGetter<typename tAttribute<tIt>::tCollapseType,
                                             tIsUnused::value,pTarget>::Get(pAttr,pDummy),pCtx,pSk);
    }
    
    template<int pElem,int pTarget,typename tIt,typename tAttr,typename tCtx,typename tSk>
    typename std::enable_if<pElem<sizeof...(tElems)-1,bool>::type
    ParseImpl(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx &pCtx,const tSk &pSk,UUnused &pDummy) const
    {
        typedef std::is_same<UUnused,typename std::tuple_element<pElem,tElemTuple>::type::template tAttribute<tIt>::tType> tIsUnused;

        if(!std::get<pElem>(mElems).Parse(pB,pE,detail::MSequenceGetter<typename tAttribute<tIt>::tCollapseType,
                                          tIsUnused::value,pTarget>::Get(pAttr,pDummy),pCtx,pSk)) 
            return false;
        
        return ParseImpl<pElem+1,pTarget+(tIsUnused::value?0:1)>(pB,pE,pAttr,pCtx,pSk,pDummy);
    }
    
    template<typename tIt,typename tAttr,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx &pCtx,const tSk &pSk) const
    {
        tIt lI=pB;
        
        UUnused lDummy;
        
        if(!ParseImpl<0,0>(lI,pE,pAttr,pCtx,pSk,lDummy))
            return false;
        
        pB=lI;
        
        return true;
    }

    tElemTuple &GetElements()               {   return mElems;  }
    const tElemTuple &GetElements() const   {   return mElems;  }

private:
    tElemTuple mElems;
};

template<typename... tArgs>
ZSequenceOp<ODecay<tArgs>...> sequence(tArgs&&... pArgs)
{
    return ZSequenceOp<ODecay<tArgs>...>(detail::nNaryExplicit,std::forward<tArgs>(pArgs)...);
}

/*namespace zz*/ } /*namespace buola*/ }

#endif
